/** ~type:text/javascript~*/
/**
 *
 * THIS FILE REQUIRES 'jquery.formvalidator.js' and 'jquery.clientsidecommunicationinterface.js' TO WORK
 */
$(function(){


    $('#container_loginType').buttonset();
    $(".loginButton").button();
    $('.errorMessageDiv').hide();

    var loginName = jQuery(':input[id=loginName]');
    var loginPassword = jQuery(':input[id=loginPassword]');

    $('#container_userType').buttonset();
    //jQuery('.ui-button-text').css('width','250px');

    var loginHiddenResponseDiv = jQuery('#loginResponseDiv');
    loginHiddenResponseDiv.hide();
    var allFields = jQuery([]).add(loginName).add(loginPassword);

        $("#dialog-box-login").dialog({
        resizable: true,
        width: 340,
        modal: true,
        show: 'slide',
//        hide: 'clip',
        autoOpen: false,
        closeOnEscape: false,
close: function(event, ui) {
allFields.val("");
parent.location.hash = '';



}
    });
    
//    $(".loginButton").click(function(event){
//         //suppress posting of form
//         event.preventDefault();
        $("#dialog-box-login").dialog( "option", "buttons", {
            "Sign In": function() {
        var bValid = true;
        allFields.removeClass('ui-state-error');

        bValid = bValid && formValidator.checkIfEmpty(loginName);
        bValid = bValid && formValidator.checkRegexp(loginName,/^([0-9a-zA-Z])+$/,"Only alphanumeric characters allowed (a-z 0-9).");
        bValid = bValid && formValidator.checkIfEmpty(loginPassword);
        bValid = bValid && formValidator.checkRegexp(loginPassword,/^([0-9a-zA-Z])+$/,"Only alphanumeric characters allowed (a-z 0-9).");

        if (bValid) {
            sendRequest.setAction('loginUser');
            var loginType1 = $("input:radio[name=loginType]:checked").val();
            loginData = {
                'loginName':loginName.val(),
                'loginPassword':loginPassword.val(),
                'loginType':loginType1
            };
            sendRequest.setSendParameters(loginData);
            loginHiddenResponseDiv.load(sendRequest.serverURL, sendRequest.getSendData(), function(){
                loginHiddenResponseDiv.unbind();
                var loginResponse = (loginHiddenResponseDiv.html()).replace(/^\s*|\s*$/g,'');
                if (loginResponse == 1)
                {
                   var loginFormAction =  $("#loginForm").attr('action');
                   if (loginType1 == 'cricketfan')
                   {
                       loginFormAction = loginFormAction +"cricketfans/";
                       $("#loginForm").attr('action',loginFormAction);
                      // $('#loginForm').submit();
                           sendRequest.setAction('getCricketFanContent');
                           sendRequest.setSendParameters(loginData);
 $("#mask").load(sendRequest.serverURL,sendRequest.getSendData(),function (cricketFanContent)
{
//commentatorContent
    var cricketFanData = jQuery.parseJSON(cricketFanContent);
   $("#leftcolumn").html(cricketFanData.side_nav);
$("#dialog-box-login").dialog('close');

});

                   }
                   else if (loginType1 == 'commentator')
                   {
                       loginFormAction = loginFormAction +"commentator/";
                       $("#loginForm").attr('action',loginFormAction);
                       //$('#loginForm').submit();
                           sendRequest.setAction('getCommentatorContent');
                           sendRequest.setSendParameters(loginData);


 $("#mask").load(sendRequest.serverURL,sendRequest.getSendData(),function (commentatorContent)
{
//commentatorContent
    var commentatorData = jQuery.parseJSON(commentatorContent);
   $("#leftcolumn").html(commentatorData.side_nav);
$("#dialog-box-login").dialog('close');
  
});
                         sendRequest.setAction('getCommentatorJSFiles');
                           sendRequest.setSendParameters(loginData);
 $("#mask").load(sendRequest.serverURL,sendRequest.getSendData(),function (commentatorContent)
{
});
                   }
                   else
                   {
                       formValidator.updateErrorMessage("Login Type is not selected.");
                   }

                }
                else
                {
                      formValidator.updateErrorMessage("User does not exist.");
                }

            });
        }
            }
    });

//    if(typeof runHomeTests == 'function') {
//        $('#homeDiv').prepend("<h3>Running Home Tests</h3><div id ='testViewer'></div>");
//        runHomeTests();
//    }


});